﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BigWoo.Apps.CertToolLib;

namespace BigWoo.Apps.CertToolGui
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public bool AddressBookType { get; set; }
        public bool AuthRootType { get; set; }
        public bool CertificateAuthorityType { get; set; }
        public bool DisallowedType { get; set; }
        public bool MyStoreType { get; set; }
        public bool RootType { get; set; }
        public bool TrustedPeopleType { get; set; }
        public bool TrustedPublisherType { get; set; }
        public bool CurrentUserLocation { get; set; }
        public bool LocalMachineLocation { get; set; }
        public CertificateDataCollection CertificateList { get; set; }

        #region ctors
        public MainWindow()
        {
            InitializeComponent();

            CertificateList = new CertificateDataCollection();
            MyStoreType = true;
            CurrentUserLocation = true;

            DataContext = this;

        } 
        #endregion

        #region private methods
        private CertificateDataCollection AggregateList(CertificateDataCollection sourceList, CertificateDataCollection additionalList)
        {
            if (null == additionalList)
                return sourceList;

            foreach (CertificateData data in additionalList)
                sourceList.Add(data);

            return sourceList;
        }

        private CertificateDataCollection BuildForLocationType(StoreLocationType location)
        {
            CertificateDataCollection returnList = new CertificateDataCollection();
            CertificateDataCollection localList = null;

            // TODO: refactor this pattern out into common code
            if (true == AddressBookType)
            {
                localList = Search.FindBy(StoreNameType.AddressBook, location);
                AggregateList(returnList, localList);
            }

            if (true == AuthRootType)
            {
                localList = Search.FindBy(StoreNameType.AuthRoot, location);
                AggregateList(returnList, localList);
            }

            if (true == CertificateAuthorityType)
            {
                localList = Search.FindBy(StoreNameType.CertificateAuthority, location);
                AggregateList(returnList, localList);
            }

            if (true == DisallowedType)
            {
                localList = Search.FindBy(StoreNameType.Disallowed, location);
                AggregateList(returnList, localList);
            }

            if (true == MyStoreType)
            {
                localList = Search.FindBy(StoreNameType.My, location);
                AggregateList(returnList, localList);
            }

            if (true == RootType)
            {
                localList = Search.FindBy(StoreNameType.Root, location);
                AggregateList(returnList, localList);
            }

            if (true == TrustedPeopleType)
            {
                localList = Search.FindBy(StoreNameType.TrustedPeople, location);
                AggregateList(returnList, localList);
            }

            if (true == TrustedPublisherType)
            {
                localList = Search.FindBy(StoreNameType.TrustedPublisher, location);
                AggregateList(returnList, localList);
            }

            return returnList;
        } 
        #endregion

        private void AltS_CommandBinding_Executed(object sender, RoutedEventArgs e)
        {
            CertificateList = new CertificateDataCollection();
            CertificateDataCollection localList = null;

            // TODO: refactor this pattern out into common code
            if (true == CurrentUserLocation)
            {
                localList = BuildForLocationType(StoreLocationType.CurrentUser);
                AggregateList(CertificateList, localList);
            }

            if (true == LocalMachineLocation)
            {
                localList = BuildForLocationType(StoreLocationType.LocalMachine);
                AggregateList(CertificateList, localList);
            }

            resultsGrid.DataContext = CertificateList;
        }

        private void AltC_CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            CertificateData selectedCert = resultsGrid.SelectedItem as CertificateData;

            if (null == selectedCert)
                return;

            Clipboard.SetText(selectedCert.ToString());
        }
    }
}
